#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sqlite3.h>
#include <pthread.h>
#include "StdTcp.h"
#include "StdThread.h"
#include "DoubleLinkList.h"
#include "ThreadPool1.h"
#include "StdSqlite.h"
struct Client
{
    int sock;
    char name[20];
};
typedef struct Client C;

C *createClient(int sock, const char *name)
{
    C *c = (C *)malloc(sizeof(C));
    if (c == NULL)
    {
        printf("createClient malloc error!\n");
        return NULL;
    }
    c->sock = sock;
    strcpy(c->name, name);
    return c;
}

void FreeClient(C *c)
{
    if (c != NULL)
    {
        free(c);
    }
}

struct Message
{
    int flag;
    char online;
    int filesize;
    char onlinename[1000];
    char fromName[20];
    char username[20];
    char password[20];
    char toName[20];
    char content[1024];
    char allname[1000];
    char groupname[100];
    char event[1024];
    char group[100];
    char grouplist[1024];
    char chat[1024];
    char filepath[100];
    char file[100];
    char filename[20];
    char filelist[1024];
};

typedef struct Message Msg;

DLlist list;
SQL *q;
sqlite3 *db1;

void ServerRegister(Msg msg, int sock, SQL *sql)
{

    if (sqlite3_open("test.db", &db1) != SQLITE_OK)
    {
        printf("open error : %s\n", sqlite3_errmsg(db1));
    }
    char temp[100] = {0};
    sprintf(temp, "insert into user values('%s','%s','%d');", msg.username, msg.password, msg.online);
    if (sqlite3_exec(db1, temp, NULL, NULL, NULL) != SQLITE_OK)
    {
        printf("insert table error :%s\n", sqlite3_errmsg(db1));
        msg.flag = 10;
    }
    else
    {
        msg.flag = 11;
        char temp1[100] = {0};
        sprintf(temp1, "'%s'", msg.username);
        char *prolist[] = {"name", "text"};
        CreateTable(sql, temp1, prolist, sizeof(prolist) / sizeof(prolist[0]) / 2);

        char temp2[100] = {0};
        sprintf(temp2, "%s_event", msg.username);
        char *prolist1[] = {"name", "text", "sign", "integer", "content", "text", "spokesman", "text"};
        CreateTable(sql, temp2, prolist1, sizeof(prolist1) / sizeof(prolist1[0]) / 2);

        char temp3[100] = {0};
        sprintf(temp3, "%s_group", msg.username);
        char *prolist2[] = {"name", "text"};
        CreateTable(sql, temp3, prolist2, sizeof(prolist2) / sizeof(prolist2[0]) / 2);
        TcpServerSend(sock, &msg, sizeof(Msg));
    }

    sqlite3_close(db1);
}
void ServerLogin(Msg msg, SQL *sql, int sock)
{
    char **result;
    int row, column;
    GetTableInfo(sql, "user", &result, &row, &column);
    msg.flag = 13;
    for (int i = 1; i <= row; i++)
    {

        // printf("%s %s\n", result[i * column], result[i * column + 1]);
        if (strcmp(msg.username, result[i * column]) == 0 && strcmp(msg.password, result[i * column + 1]) == 0)
        {
            msg.flag = 12;
            char n[20] = {0};
            strcat(n, "'");
            strcat(n, msg.username);
            strcat(n, "'");
            char temp[20] = {0};
            strcpy(temp, "name=");
            strcat(temp, n);
            UpdateData(sql, "user", "online=1", temp);
            strcpy(msg.fromName, msg.username);
            InsertTail(&list, createClient(sock, msg.fromName));
            FreeInfoResult(result);

            break;
        }
    }
    TcpServerSend(sock, &msg, sizeof(msg));
}
void *thread_handler(void *arg)
{

    int sock = *(int *)arg;
    Msg msg;
    SQL *sql = InitSqlite("test.db");
    while (1)
    {

        memset(&msg, 0, sizeof(Msg));
        TcpServerRecv(sock, &msg, sizeof(Msg));
        printf("recv flag: %d ,username: %s ,password :%s ,online :%d\n",
               msg.flag, msg.username, msg.password, msg.online);

        switch (msg.flag)
        {
        case 1:
            ServerRegister(msg, sock, sql);
            break;

        case 2:
            ServerLogin(msg, sql, sock);
            break;
        case 3:

            char **result1;
            int row1, column1;
            GetTableInfo(sql, "user", &result1, &row1, &column1);
            // msg.flag = 15;
            for (int i = 1; i <= row1; i++)
            {

                if (strcmp(msg.toName, result1[i * column1]) == 0)
                {

                    if (sqlite3_open("test.db", &db1) != SQLITE_OK)
                    {
                        printf("open error : %s\n", sqlite3_errmsg(db1));
                    }
                    char temp1[100] = {0};

                    sprintf(temp1, "insert into %s_event values('%s',2,NULL,NULL);", msg.toName, msg.fromName);

                    if (sqlite3_exec(db1, temp1, NULL, NULL, NULL) != SQLITE_OK)
                    {
                        printf("insert table error :%s\n", sqlite3_errmsg(db1));
                    }
                    sqlite3_close(db1);

                    FreeInfoResult(result1);
                }
            }

            break;
        case 4:
            char **result2;
            int row2, column2;
            msg.flag = 100;
            char m[20] = {0};
            sprintf(m, "%s", msg.username);
            char **result3;
            int row3, column3;
            GetTableInfo(sql, m, &result3, &row3, &column3);
            GetTableInfo(sql, "user", &result2, &row2, &column2);
            if (row3 == 0)
            {
                msg.flag = 651;
                TcpServerSend(sock, &msg, sizeof(msg));
            }
            for (int i = 1; i <= row2; i++)
            {
                for (int j = 1; j <= row3; j++)
                {

                    if (atoi(result2[i * column2 + 2]) == 1)
                    {

                        if (strcmp(result2[i * column2], result3[j * column3]) == 0)
                        {
                            strcat(msg.onlinename, result2[i * column2]);
                            strcat(msg.onlinename, "*");
                        }
                    }
                }
            }
            FreeInfoResult(result3);
            TcpServerSend(sock, &msg, sizeof(msg));
            break;

        case 5:
            char n[20] = {0};
            strcat(n, "'");
            strcat(n, msg.username);
            strcat(n, "'");
            char temp[20] = {0};
            strcpy(temp, "name=");
            strcat(temp, n);
            UpdateData(sql, "user", "online=0", temp);

            pthread_exit(NULL);
            // TcpServerSend(sock, &msg, sizeof(msg));

            break;
        case 6:
            char **result8;
            int row8, column8;
            msg.flag = 320;
            memset(msg.grouplist, 0, sizeof(msg.grouplist));
            char c[200] = {0};
            sprintf(c, "%s", msg.groupname);
            GetTableInfo(sql, c, &result8, &row8, &column8);
            for (int i = 1; i <= row8; i++)
            {
                strcat(msg.grouplist, result8[i * column8]);
                strcat(msg.grouplist, "*");
            }
            FreeInfoResult(result8);
            TcpServerSend(sock, &msg, sizeof(msg));

            break;
        case 7:

            if (sqlite3_open("test.db", &db1) != SQLITE_OK)
            {
                printf("open error : %s\n", sqlite3_errmsg(db1));
            }
            char temp1[2000] = {0};

            sprintf(temp1, "insert into %s_event values('%s',1,'%s',NULL);", msg.toName, msg.fromName, msg.content);

            if (sqlite3_exec(db1, temp1, NULL, NULL, NULL) != SQLITE_OK)
            {
                printf("insert table error :%s\n", sqlite3_errmsg(db1));
            }
            sqlite3_close(db1);

            if (sqlite3_open("test.db", &db1) != SQLITE_OK)
            {
                printf("open error : %s\n", sqlite3_errmsg(db1));
            }
            char temp6[2000] = {0};

            sprintf(temp6, "insert into chat values('%s','%s','%s');", msg.fromName, msg.toName, msg.content);

            if (sqlite3_exec(db1, temp6, NULL, NULL, NULL) != SQLITE_OK)
            {
                printf("insert table error :%s\n", sqlite3_errmsg(db1));
            }

            sqlite3_close(db1);

            break;

        case 8:

            struct Node *travelPoint2 = list.head;
            while (travelPoint2 != NULL)
            {
                C *c = (C *)travelPoint2->data;
                if (strcmp(c->name, msg.toName) != 0)
                {

                    TcpServerSend(c->sock, &msg, sizeof(msg));
                }
                travelPoint2 = travelPoint2->next;
            }
            break;
        case 20:
            char **result5;
            int row5, column5;
            char m4[200] = {0};
            sprintf(m4, "%s_event", msg.username);
            GetTableInfo(sql, m4, &result5, &row5, &column5);

            // if (row5 == 0)
            // {
            //     msg.flag = 206;
            //     TcpServerSend(sock, &msg, sizeof(msg));
            // }
            for (int i = 1; i <= row5; i++)
            {

                if (atoi(result5[i * column5 + 1]) == 1)
                {
                    memset(msg.event, 0, sizeof(msg));
                    memset(msg.content, 0, sizeof(msg));
                    msg.flag = 202;
                    strcpy(msg.event, result5[i * column5]);
                    strcpy(msg.content, result5[i * column5 + 2]);
                    TcpServerSend(sock, &msg, sizeof(msg));
                    TcpServerRecv(sock, &msg, sizeof(msg));
                    if (msg.flag == 51)
                    {
                        char demo8[100] = {0};
                        sprintf(demo8, "%s_event", msg.username);
                        char demo9[100] = {0};
                        sprintf(demo9, "name='%s'", result5[i * column5]);
                        DeleteData(sql, demo8, demo9);
                    }
                }

                if (atoi(result5[i * column5 + 1]) == 2)
                {
                    memset(msg.event, 0, sizeof(msg));
                    msg.flag = 14;
                    strcpy(msg.event, result5[i * column5]);
                    printf("%s\n", msg.event);
                    TcpServerSend(sock, &msg, sizeof(msg));
                    TcpServerRecv(sock, &msg, sizeof(msg));
                    if (msg.flag == 6)
                    {
                        if (sqlite3_open("test.db", &db1) != SQLITE_OK)
                        {
                            printf("open error : %s\n", sqlite3_errmsg(db1));
                        }
                        char temp1[2000] = {0};

                        sprintf(temp1, "insert into %s values('%s');", msg.username, result5[i * column5]);
                        printf("%s\n", temp1);
                        if (sqlite3_exec(db1, temp1, NULL, NULL, NULL) != SQLITE_OK)
                        {
                            printf("insert table error :%s\n", sqlite3_errmsg(db1));
                        }
                        sqlite3_close(db1);

                        if (sqlite3_open("test.db", &db1) != SQLITE_OK)
                        {
                            printf("open error : %s\n", sqlite3_errmsg(db1));
                        }
                        char temp2[2000] = {0};

                        sprintf(temp2, "insert into %s values('%s');", result5[i * column5], msg.username);
                        printf("%s\n", temp2);
                        if (sqlite3_exec(db1, temp2, NULL, NULL, NULL) != SQLITE_OK)
                        {
                            printf("insert table error :%s\n", sqlite3_errmsg(db1));
                        }
                        char temp6[100] = {0};
                        sprintf(temp6, "%s_event", msg.username);
                        char temp7[100] = {0};
                        sprintf(temp7, "name='%s'", result5[i * column5]);
                        DeleteData(sql, temp6, temp7);
                        sqlite3_close(db1);
                    }
                    if (msg.flag == 7)
                    {
                        char temp8[100] = {0};
                        sprintf(temp8, "%s_event", msg.username);
                        char temp9[100] = {0};
                        sprintf(temp9, "name='%s'", result5[i * column5]);
                        DeleteData(sql, temp8, temp9);
                    }
                }
            }

            msg.flag = 206;
            TcpServerSend(sock, &msg, sizeof(msg));

            FreeInfoResult(result5);
            break;
        case 40:
            char demo1[20] = {0};
            sprintf(demo1, "%s", msg.username);

            char demo2[40] = {0};
            sprintf(demo2, "name='%s'", msg.toName);
            DeleteData(sql, demo1, demo2);
            char demo3[20] = {0};
            sprintf(demo3, "%s", msg.toName);
            char demo4[40] = {0};
            sprintf(demo4, "name='%s'", msg.username);
            DeleteData(sql, demo3, demo4);
            msg.flag = 41;
            TcpServerSend(sock, &msg, sizeof(msg));

            break;
        case 60:
            msg.flag = 61;
            char temp9[200] = {0};
            sprintf(temp9, "'%s'", msg.groupname);
            char *prolist[] = {"name", "text", "power", "integer", "speak", "integer"};
            CreateTable(sql, temp9, prolist, sizeof(prolist) / sizeof(prolist[0]) / 2);
            char temp22[200] = {0};
            sprintf(temp22, "%s_file", msg.groupname);
            char *prolist2[] = {"groupname", "text", "fromname", "text", "filename", "text", "filecontent", "text", "filesize", "integer"};
            CreateTable(sql, temp22, prolist2, sizeof(prolist2) / sizeof(prolist2[0]) / 2);

            if (sqlite3_open("test.db", &db1) != SQLITE_OK)
            {
                printf("open error : %s\n", sqlite3_errmsg(db1));
            }
            char temp12[2000] = {0};

            sprintf(temp12, "insert into %s values('%s',1,1);", msg.groupname, msg.username);

            if (sqlite3_exec(db1, temp12, NULL, NULL, NULL) != SQLITE_OK)
            {
                printf("insert table error :%s\n", sqlite3_errmsg(db1));
            }
            sqlite3_close(db1);

            if (sqlite3_open("test.db", &db1) != SQLITE_OK)
            {
                printf("open error : %s\n", sqlite3_errmsg(db1));
            }
            char temp13[2000] = {0};

            sprintf(temp13, "insert into %s_group values('%s');", msg.username, msg.groupname);

            if (sqlite3_exec(db1, temp13, NULL, NULL, NULL) != SQLITE_OK)
            {
                printf("insert table error :%s\n", sqlite3_errmsg(db1));
            }
            sqlite3_close(db1);
            TcpServerSend(sock, &msg, sizeof(Msg));
            break;
        case 63:
            if (sqlite3_open("test.db", &db1) != SQLITE_OK)
            {
                printf("open error : %s\n", sqlite3_errmsg(db1));
            }
            char temp11[2000] = {0};

            sprintf(temp11, "insert into %s values('%s',0,1);", msg.groupname, msg.toName);

            if (sqlite3_exec(db1, temp11, NULL, NULL, NULL) != SQLITE_OK)
            {
                printf("insert table error :%s\n", sqlite3_errmsg(db1));
            }
            sqlite3_close(db1);

            if (sqlite3_open("test.db", &db1) != SQLITE_OK)
            {
                printf("open error : %s\n", sqlite3_errmsg(db1));
            }
            char temp14[2000] = {0};

            sprintf(temp14, "insert into %s_group values('%s');", msg.toName, msg.groupname);

            if (sqlite3_exec(db1, temp14, NULL, NULL, NULL) != SQLITE_OK)
            {
                printf("insert table error :%s\n", sqlite3_errmsg(db1));
            }
            sqlite3_close(db1);

            break;
        case 64:
            msg.flag = 65;
            char **result6;
            int row6, column6;
            char m2[100] = {0};
            memset(msg.group, 0, sizeof(msg.group));
            sprintf(m2, "%s_group", msg.username);
            GetTableInfo(sql, m2, &result6, &row6, &column6);
            for (int i = 1; i <= row6; i++)
            {
                msg.flag = 66;
                strcat(msg.group, result6[i * column6]);
                strcat(msg.group, "*");

                TcpServerSend(sock, &msg, sizeof(Msg));
            }
            FreeInfoResult(result6);
            break;
        case 67:
            // msg.flag = 68;
            int send = 1;
            char **result7;
            int row7, column7;
            char m3[100] = {0};
            sprintf(m3, "%s", msg.groupname);
            GetTableInfo(sql, m3, &result7, &row7, &column7);
            for (int i = 1; i <= row7; i++)
            {
                if (strcmp(result7[i * column7], msg.username) == 0)
                {
                    msg.flag = 69;
                    send = 0;
                    TcpServerSend(sock, &msg, sizeof(Msg));
                }
            }
            if (send == 1)
            {
                msg.flag = 68;
                TcpServerSend(sock, &msg, sizeof(Msg));
            }

            FreeInfoResult(result7);
            break;
        case 70:
            char **result13;
            int row13, column13;
            char b[2000] = {0};
            msg.flag = 71;
            memset(msg.chat, 0, sizeof(msg.chat));
            GetTableInfo(sql, "chat", &result13, &row13, &column13);
            for (int i = 1; i <= row13; i++)
            {
                if (strcmp(result13[i * column13], msg.username) == 0 && strcmp(result13[i * column13 + 1], msg.toName) == 0)
                {
                    memset(b, 0, sizeof(b));
                    sprintf(b, "向%s发送了:%s", result13[i * column13 + 1], result13[i * column13 + 2]);
                    strcat(msg.chat, b);
                    strcat(msg.chat, "*");
                }
            }
            FreeInfoResult(result13);
            TcpServerSend(sock, &msg, sizeof(Msg));
            break;
        case 200:
            msg.flag = 201;
            char **result4;
            int row4, column4;
            char m1[20] = {0};
            sprintf(m1, "%s", msg.username);
            GetTableInfo(sql, m1, &result4, &row4, &column4);
            if (row4 == 0)
            {
                msg.flag = 650;
                TcpServerSend(sock, &msg, sizeof(msg));
            }
            else
            {
                for (int i = 1; i <= row4; i++)
                {
                    strcat(msg.allname, result4[i * column4]);
                    strcat(msg.allname, "*");
                }
                FreeInfoResult(result4);
                TcpServerSend(sock, &msg, sizeof(msg));
            }
            break;
        case 300:
            char **result9;
            int row9, column9;
            char m5[200] = {0};
            sprintf(m5, "%s", msg.groupname);
            GetTableInfo(sql, m5, &result9, &row9, &column9);
            for (int i = 1; i <= row9; i++)
            {
                if (strcmp(result9[i * column9], msg.username) == 0)
                {
                    if (atoi(result9[i * column9 + 2]) == 1)
                    {
                        for (int j = 1; j <= row9; j++)
                        {
                            // memset(msg.spokesman,0,sizeof(msg.spokesman));
                            if (strcmp(result9[j * column9], msg.username) == 0)
                            {
                                continue;
                            }

                            if (sqlite3_open("test.db", &db1) != SQLITE_OK)
                            {
                                printf("open error : %s\n", sqlite3_errmsg(db1));
                            }
                            char temp22[2000] = {0};

                            sprintf(temp22, "insert into %s_event values('%s',3,'%s','%s');", result9[j * column9], msg.groupname, msg.content, msg.username);

                            if (sqlite3_exec(db1, temp22, NULL, NULL, NULL) != SQLITE_OK)
                            {
                                printf("insert table error :%s\n", sqlite3_errmsg(db1));
                            }
                            sqlite3_close(db1);
                        }
                    }
                    else
                    {
                        msg.flag = 89;
                        TcpServerSend(sock, &msg, sizeof(msg));
                    }
                }
            }
            FreeInfoResult(result9);
            break;
        case 400:
            char **result10;
            int row10, column10;
            char m6[200] = {0};

            sprintf(m6, "%s_event", msg.username);
            GetTableInfo(sql, m6, &result10, &row10, &column10);
            // if (row10 == 0)
            // {
            //     msg.flag = 206;
            //     TcpServerSend(sock, &msg, sizeof(msg));
            // }
            for (int j = 0; j <= row10; j++)
            {

                if (atoi(result10[j * column10 + 1]) == 3)
                {

                    msg.flag = 401;
                    memset(msg.event, 0, sizeof(msg.event));
                    memset(msg.content, 0, sizeof(msg.content));
                    memset(msg.fromName, 0, sizeof(msg.fromName));
                    strcpy(msg.event, result10[j * column10]);
                    strcpy(msg.content, result10[j * column10 + 2]);
                    strcpy(msg.fromName, result10[j * column10 + 3]);

                    TcpServerSend(sock, &msg, sizeof(msg));
                    TcpServerRecv(sock, &msg, sizeof(msg));
                    if (msg.flag == 23)
                    {
                        char temp15[100] = {0};
                        sprintf(temp15, "%s_event", msg.username);
                        char temp16[100] = {0};
                        sprintf(temp16, "name='%s'", result10[j * column10]);
                        DeleteData(sql, temp15, temp16);
                    }
                }
            }
            msg.flag = 206;
            TcpServerSend(sock, &msg, sizeof(msg));
            FreeInfoResult(result10);
            break;
        case 410:
            char **result11;
            int row11, column11;
            char m7[200] = {0};
            sprintf(m7, "%s", msg.groupname);
            GetTableInfo(sql, m7, &result11, &row11, &column11);
            for (int j = 0; j <= row11; j++)
            {
                if (atoi(result11[j * column11 + 1]) == 1)
                {

                    char n[40] = {0};
                    sprintf(n, "name='%s'", msg.toName);
                    UpdateData(sql, m7, "speak=0", n);
                }
                else
                {
                    msg.flag = 411;
                    TcpServerSend(sock, &msg, sizeof(msg));
                }
            }
            FreeInfoResult(result11);
            break;
        case 420:
            char **result12;
            int row12, column12;
            char m8[200] = {0};
            sprintf(m8, "%s", msg.username);
            GetTableInfo(sql, m8, &result12, &row12, &column12);
            for (int j = 0; j <= row12; j++)
            {
                if (strcmp(result12[j * column12], msg.toName) == 0)
                {
                    if (sqlite3_open("test.db", &db1) != SQLITE_OK)
                    {
                        printf("open error : %s\n", sqlite3_errmsg(db1));
                    }
                    char temp16[2000] = {0};

                    sprintf(temp16, "insert into %s_group values('%s');", msg.toName, msg.groupname);

                    if (sqlite3_exec(db1, temp16, NULL, NULL, NULL) != SQLITE_OK)
                    {
                        printf("insert table error :%s\n", sqlite3_errmsg(db1));
                    }
                    sqlite3_close(db1);

                    if (sqlite3_open("test.db", &db1) != SQLITE_OK)
                    {
                        printf("open error : %s\n", sqlite3_errmsg(db1));
                    }
                    char temp17[2000] = {0};

                    sprintf(temp17, "insert into %s values('%s',0,1);", msg.groupname, msg.toName);

                    if (sqlite3_exec(db1, temp17, NULL, NULL, NULL) != SQLITE_OK)
                    {
                        printf("insert table error :%s\n", sqlite3_errmsg(db1));
                    }
                    sqlite3_close(db1);
                    msg.flag = 421;
                    TcpServerSend(sock, &msg, sizeof(msg));
                }
            }
            FreeInfoResult(result12);
            break;
        case 430:
            char **result22;
            int row22, column22;
            char m21[200] = {0};
            sprintf(m21, "%s", msg.groupname);
            GetTableInfo(sql, m21, &result22, &row22, &column22);
            for (int j = 0; j <= row22; j++)
            {
                if (atoi(result11[j * column11 + 1]) == 1)
                {
                    char temp17[100] = {0};
                    sprintf(temp17, "%s", msg.groupname);
                    char temp18[30] = {0};
                    sprintf(temp18, "name='%s'", msg.toName);
                    DeleteData(sql, temp17, temp18);
                    char temp19[30] = {0};
                    sprintf(temp19, "%s_group", msg.toName);
                    char temp20[200] = {0};
                    sprintf(temp20, "name='%s'", msg.groupname);
                    DeleteData(sql, temp19, temp20);
                    msg.flag = 431;
                    TcpServerSend(sock, &msg, sizeof(msg));
                }
                else
                {
                    msg.flag = 411;
                    TcpServerSend(sock, &msg, sizeof(msg));
                }
            }
            break;
        case 440:

            if (sqlite3_open("test.db", &db1) != SQLITE_OK)
            {
                printf("open error : %s\n", sqlite3_errmsg(db1));
            }
            char temp24[2000] = {0};

            sprintf(temp24, "insert into %s_file values('%s','%s','%s','%s',%d);", msg.groupname, msg.groupname, msg.username, msg.filename, msg.file, msg.filesize);

            if (sqlite3_exec(db1, temp24, NULL, NULL, NULL) != SQLITE_OK)
            {
                printf("insert table error :%s\n", sqlite3_errmsg(db1));
            }
            sqlite3_close(db1);

            // TcpServerSend(sock, &msg, sizeof(msg));
            break;
        case 450:
            char **result67;
            int row67, column67;
            char m9[200] = {0};
            sprintf(m9, "%s_file", msg.groupname);
            GetTableInfo(sql, m9, &result67, &row67, &column67);
            if (row67 == 0)
            {
                msg.flag = 452;
                TcpServerSend(sock, &msg, sizeof(msg));
            }
            else
            {
                msg.flag = 451;
                for (int i = 1; i <= row67; i++)
                {
                    printf("ss:%s\n", result67[i * column67 + 2]);
                    strcat(msg.filelist, result67[i * column67 + 2]);
                    strcat(msg.filelist, "*");
                }
                TcpServerSend(sock, &msg, sizeof(msg));
            }
            // msg.flag = 452;
            // TcpServerSend(sock, &msg, sizeof(msg));
            break;
        case 460:
            char **result15;
            int row15, column15;
            char m10[200] = {0};
            sprintf(m10, "%s_file", msg.groupname);
            GetTableInfo(sql, m10, &result15, &row15, &column15);
            for (int i = 0; i <= row15; i++)
            {
                if (strcmp(result15[i * column15 + 2], msg.filename) == 0)
                {
                    memset(msg.file, 0, sizeof(msg.file));
                    msg.flag = 461;
                    msg.filesize = atoi(result15[i * column15 + 4]);
                    strcpy(msg.file, result15[i * column15 + 3]);

                    TcpServerSend(sock, &msg, sizeof(msg));
                }
            }
            break;
        default:
            break;
        }
    }
}

int main(int argc, char *argv[])
{
    if (argc != 3)
    {
        printf("invaild numbers!\n");
        return -1;
    }

    InitDLlist(&list);

    ThreadP *p = InitThreadPool(10, 20, 10);
    if (p == NULL)
    {
        printf("server point is NULL!\n");
        return -1;
    }

    Tcps *s = InitTcpServer(argv[1], atoi(argv[2]));
    if (s == NULL)
    {
        printf("server point is NULL!\n");
        return -1;
    }
    SQL *q = InitSqlite("test.db");
    // char *prolist[] = {"name", "text", "password", "text", "online", "integer"};
    // CreateTable(q, "user", prolist, sizeof(prolist) / sizeof(prolist[0]) / 2);
    // char *prolist[] = {"fromname", "text", "toname", "text", "content", "text"};
    // CreateTable(q, "chat", prolist, sizeof(prolist) / sizeof(prolist[0]) / 2);
    int acceptSock = 0;
    while ((acceptSock = TcpServerAccrpt(s)) > 0)
    {
        ThreadP_AddTask(p, thread_handler, &acceptSock);
    }
    // DestoryThreadPool(p);

    return 0;
}
